<!-- 蓝色简洁登录页面 -->
<template>
	<view class="login-bg">
		<view class="statbar"></view>
		<u-navbar title="登录" :autoBack="true"></u-navbar>

		<br /><br /><br /><br /><br /><br /><br />
		<view class="t-login">
			<form class="cl">
				<view class="t-a">
					<image src="@/static/user.png"></image>
					<input type="string" name="username" placeholder="请输入用户名" maxlength="11" v-model="username" />
				</view>
				<view class="t-a">
					<image src="@/static/pwd.png"></image>
					<input type="password" name="password" maxlength="6" placeholder="请输入密码" v-model="password" />
				</view>
				<button @click="login()">登 录</button>
				<view class="t-c">
					<text class="t-c-txt" @click="reg()">注册账号</text>
					<text @click="forgotPwd()">忘记密码</text>
				</view>
			</form>
			<!-- <view class="t-f"><text>—————— 其他登录方式 ——————</text></view>
			<view class="t-e cl">
				<view class="t-g" @tap="wxogin()">
					<image src="@/static/wx2.png"></image>
				</view>
				<view class="t-g" @tap="qqLogin()">
					<image src="@/static/qq2.png"></image>
				</view>
				<view class="t-g" @tap="wbLogin()">
					<image src="@/static/wb.png"></image>
				</view>
			</view> -->
		</view>
		<image class="img-a" src="@/static/bg1.png"></image>
	</view>
</template>
<script>
	import {
		getLoginRsaKey,
		login,
		getUserInfo,
		appPush
	} from '@/config/api.js';

	import {
		SOCKET_URL
	} from '@/config/config.js';
	export default {
		data() {
			return {
				phone: '', //手机号码
				username: '', //用户名
				password: '', //密码

			};
		},
		onLoad(option) {},
		methods: {
			//当前登录按钮操作
			login() {
				var that = this;
				// if (!that.phone) {
				// 	uni.showToast({ title: '请输入手机号', icon: 'none' });
				// 	return;
				// }
				// if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.phone)) {
				// 	uni.showToast({ title: '请输入正确手机号', icon: 'none' });
				// 	return;
				// }


				if (!that.username) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}
				if (!that.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				getLoginRsaKey().then(res => {
					let encrypt = new that.$jsencrypt();
					encrypt.setPublicKey(res.publicKey);
					let passwd = encrypt.encrypt(this.password);
					let user = {
						username: this.username,
						passwd: passwd,
						rsakey: res.rsakey
					}
					login(user)
						.then(data => {
							uni.$emit('sign_in', data.userId)
							//that.connectSocket(data.userId)
							uni.showToast({
								title: '登录成功！',
								icon: 'none'
							});
							this.$store.commit('SetToken', data.token);
							this.$store.commit('SetUserId', data.userId);
							this.$store.commit('SetUser', data.user);		
							uni.switchTab({
								url: '../my/my'
							});
						})
						.catch(() => {
							uni.showToast({
								title: '登录失败！',
								icon: 'none'
							});

							//this.getCode();
						});

				})

			},
			//忘记密码
			forgotPwd() {
				uni.showToast({
					title: '忘记密码',
					icon: 'none'
				});
			},
			//立刻注册
			reg() {
				uni.$u.route({
					url: 'pages/register/register',
					params: {}
				})
			},
			getCode() {
				// getCode().then(data => {
				// 	this.codeUrl = 'data:image/jpg;base64,' + data.img.replace(/[\r\n]/g, '');
				// 	this.loginForm.uuid = data.uuid;
				// });
			},
			// connectSocket(userId) {
			// 	uni.connectSocket({
			// 		url: SOCKET_URL + `/ws/` + userId,
			// 		success: res => {
			// 			uni.onSocketOpen(() => {
			// 				console.log('WebSocket连接已打开！');
			// 			});
			// 			uni.onSocketMessage(result => {
			// 				console.log('服务器接收内容');
			// 				console.log(result);

			// 				let data = JSON.parse(result.data);
			// 				uni.$emit('chat_msg', result,data)

			// 				console.log(data);
			// 				if (typeof data.msg !== 'undefined') {
			// 					this.$store.commit('SetMessages', data.msg);
			// 				}
			// 				this.$store.commit('SetUserChatList', data.userList);
			// 			});
			// 			uni.onSocketError(err => {
			// 				uni.showToast({
			// 					title: 'WebSocket连接发生错误！',
			// 					icon: 'none',
			// 					duration: 2000
			// 				});
			// 				console.log(err);
			// 			});
			// 			uni.onSocketClose(close => {
			// 				uni.showToast({
			// 					title: 'webSocket 已关闭！',
			// 					icon: 'none',
			// 					duration: 2000
			// 				});
			// 				console.log('WebSocket 已关闭！');
			// 			});
			// 		},
			// 		fail: () => {
			// 			uni.showToast({
			// 				title: '连接失败，请检查网络',
			// 				icon: 'none',
			// 				duration: 2000
			// 			});
			// 		}
			// 	});
			//}

		}
	};
</script>
<style>
	.statbar {
		height: var(--status-bar-height);
		background: linear-gradient(to right, #ff8f77, #fe519f);
	}

	.img-a {
		width: 100%;
		position: absolute;
		bottom: 0;
	}

	.login-bg {
		height: 100vh;
		background-image: url(../../static/bg3.png);
	}

	.t-login {
		width: 580rpx;
		padding: 55rpx;
		margin: 0 auto;
		font-size: 28rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		box-shadow: 0 5px 7px 0 rgba(0, 0, 0, 0.15);
		z-index: 9;
	}

	.t-login button {
		font-size: 28rpx;
		background: linear-gradient(to right, #ff8f77, #fe519f);
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
	}

	.t-login input {
		padding: 0 20rpx 0 120rpx;
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 50rpx;
		background: #f6f6f6;
		border: 1px solid #f6f6f6;
		font-size: 28rpx;
		border-radius: 50rpx;
	}

	.t-login .t-a {
		position: relative;
	}

	.t-login .t-a image {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		left: 40rpx;
		top: 28rpx;
		padding-right: 20rpx;
	}

	.t-login .t-b {
		text-align: left;
		font-size: 46rpx;
		color: #000;
		padding: 300rpx 0 120rpx 0;
		font-weight: bold;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-c {
		text-align: right;
		color: #666666;
		margin: 30rpx 30rpx 40rpx 0;
	}

	.t-login .t-c .t-c-txt {
		margin-right: 300rpx;
	}

	.t-login .t-e {
		text-align: center;
		width: 600rpx;
		margin: 40rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 33.33%;
	}

	.t-login .t-e image {
		width: 70rpx;
		height: 70rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin: 80rpx 0 0 0;
		color: #999;
	}

	.t-login .t-f text {
		margin-left: 20rpx;
		color: #b9b9b9;
		font-size: 27rpx;
	}

	.t-login .uni-input-placeholder {
		color: #aeaeae;
	}

	.cl {
		zoom: 1;
	}

	/* .cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	} */
</style>
